import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-02-14
 * Time: 14:40
 */
public class test1 {
    /**
     * LeetCode 394  字符串编码
     */

    // 利用双栈解决
    public String decodeString(String s) {
        // 数字栈
        Stack<Integer> numStack = new Stack<>();
        // 字符栈
        Stack<String> stringStack = new Stack<>();

        int num =0;
        // 用于拼接
        String curString = "";
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if(Character.isDigit(c)){
                // 这里num*10，是怕可能会有多位数，例如10，100等
                num = num*10+c-'0';
            }else if(c=='['){
                numStack.push(num);
                stringStack.push(curString);
                // 重置
                num=0;
                curString="";
            }else if(c==']'){
                // 记录要循环几次
                int loopTimes = numStack.pop();
                StringBuilder tmp = new StringBuilder(stringStack.pop());
                for(int j=0;j<loopTimes;j++){
                    tmp.append(curString);
                }
                curString = tmp.toString();
            }else{
                curString+=c;
            }
        }
        return curString;
    }
}
